/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package base;

import tal.drivechain.matchers.VarMatcher;
import tal.drivechain.plugin.Channel;
import tal.drivechain.plugin.Module;

import tal.drivechain.variable.types.DataType;
import tal.drivechain.variable.SetProperty;
import tal.drivechain.variable.VarTableAction;
import tal.drivechain.variable.Variable;

/**
 *
 * @author Main
 */
public class Smooth extends Module {

    private VarMatcher trigger;
    private Variable smoothVar, out;
    private VarTableAction setSmooth;
    
    private double[] buffer;
    private int bufloc = 0, smoothRange;
    
    @Override
    public void setupProperties() {
        setTrigger(props.getProperty("trigger"), null);
        setOutput(props.getProperty("output"), null);
        setBufferSize(props.getProperty("bufferSize"), null);

        smoothVar = new Variable("smooth", DataType.FLOAT, null, true);
        setSmooth = new VarTableAction("smooth", VarTableAction.Action.SET_ANY);

    }

    public void setOutput(Object value, SetProperty.Flag flag) {
        out = (Variable)value;
    } public Object getOutput() { return out; }
    
    public void setTrigger(Object value, SetProperty.Flag flag) {
        trigger = (VarMatcher)value;
    } public Object getTrigger() { return trigger; }
    
    public void setBufferSize(Object value, SetProperty.Flag flag) {
        int bufsize = (Integer)value;
        if (bufsize%2==0) throw new IllegalArgumentException("bufferSize must be an odd number.");
        smoothRange = (bufsize-1)/2;
        buffer = new double[bufsize];
    } public Object getBufferSize() { return buffer.length; }

    @Override
    protected void msgReceived(Variable in, long timestamp, Channel channel) {
        if (trigger.matches(in, channel, this)) {
            double curValue;
            if (in.getDataType().matches(DataType.FLOAT))
                curValue = (Double)in.getValue(this);
            else
                curValue = (Double)DataType.FLOAT.cast(in.getValue(this));
            if (Double.isNaN(curValue)) curValue = 0;
            manageBuffer(curValue);
        }
    }

    private void manageBuffer(double curValue) {
        if (bufloc>=buffer.length) bufloc = 0;
        buffer[bufloc] = curValue;
        
        double[] befores = new double[smoothRange];
        double[] afters = new double[smoothRange];
        
        boolean negativeLoc;
        int smoothloc = bufloc - smoothRange;
        if (smoothloc<0) {
            smoothloc = buffer.length + smoothloc;
            negativeLoc = true;
        } else negativeLoc = false;
        
        // the ones before are: bufloc-(bufsize-1)/2..bufloc-1
        // the ones after are: bufloc+1..(bufloc+(bufsize-1)/2)

        // get the ones before: bufloc-smoothrange..bufloc-1
        int beforesIdx = 0;
        if (!negativeLoc) {
            for (int i=bufloc+1; i<buffer.length; i++) {
                befores[beforesIdx] = buffer[i];
                beforesIdx++;
            } for (int i=0; i<smoothloc; i++) {
                befores[beforesIdx] = buffer[i];
                beforesIdx++;
            }
        } else {
            for (int i=bufloc+1; i<smoothloc; i++) {
                befores[beforesIdx] = buffer[i];
                beforesIdx++;
            }
        }
        
        // put everything between smoothloc and bufloc including
        int aftersIdx = 0;
        if (!negativeLoc)
            for (int i=smoothloc+1; i<=bufloc; i++) {
                afters[aftersIdx] = buffer[i];
                aftersIdx++;
            }
        else {
            for (int i=smoothloc+1; i<buffer.length; i++) {
                afters[aftersIdx] = buffer[i];
                aftersIdx++;
            }
            for (int i=0; i<=bufloc; i++) {
                afters[aftersIdx] = buffer[i];
                aftersIdx++;
            }
        }
           
        /*System.out.print("buffer = [");
        for (int i=0; i<befores.length; i++) {
            System.out.print(befores[i] + ",");
        }
        System.out.print("<<<" + buffer[smoothloc] + ">>>,");
        for (int i=0; i<befores.length; i++) {
            System.out.print(afters[i] + ",");
        }        
        System.out.println("]");*/
        buffer[smoothloc] = smooth(buffer[smoothloc], afters, befores);
        smoothVar.setValue((Double)buffer[smoothloc], true);
        setSmooth.set(smoothVar, localVars, this);
        this.sendToOutputs(out, -1);
        //System.out.println("out==" + buffer[smoothloc]);
        bufloc++;
    }

    private double smooth(double value, double[] afters, double[] befores) {
        double up = 0;
        for (int i=0; i<befores.length; i++) up+=befores[i];
        up+=value;
        for (int i=0; i<afters.length; i++) up+=afters[i];
        return (double)(up/buffer.length);
    }
}
 